{-----------------------------------------------------------------------------
 Unit Name: IteratorImpl
 Purpose:

 Author:    Nikolay Nenov http://nenov.de
 Date:      11-05-2011
 History:
-----------------------------------------------------------------------------}
unit IteratorImpl;

interface

uses classes, controls, Iterator;

type
  TIteratorData = array of TObject;

  TIteratorImpl = class (TInterfacedObject, IIterator)
  private
    Data        : TIteratorData;
    DataPoint   : ^TIteratorData;
    ItemIndex   : Integer;
    IsFirst     : Boolean;
  public
    constructor Create(AData: array of TObject);
    function Next      : TObject;
    function HasNext  : Boolean;
  end;

implementation


{-----------------------------------------------------------------------------
  Procedure: Create
  Author:    Nikolay Nenov
  Date:      11-05-2011
  Arguments: MainObj : TControl

-----------------------------------------------------------------------------}
constructor TIteratorImpl.Create(AData: array of TObject);
var
  I: Integer;
begin
  ItemIndex := 0;
  DataPoint := @AData;
  Data      := DataPoint^;
  IsFirst   := True;
end;


{-----------------------------------------------------------------------------
  Procedure: Next
  Author:    Nikolay Nenov
  Date:      17-05-2011
  Arguments:
-----------------------------------------------------------------------------}
function TIteratorImpl.Next: TObject;
begin
  if IsFirst then
  begin
    IsFirst := False;
    Result := Data[ItemIndex];
  end
  else
  begin
    if HasNext then
      Inc(ItemIndex);
    Result := Data[ItemIndex];
  end;
end;


{-----------------------------------------------------------------------------
  Function:  HasNext
  Author:    Nikolay Nenov
  Date:      17-05-2011
  Arguments:
  Result:    Boolean
-----------------------------------------------------------------------------}
function TIteratorImpl.HasNext: Boolean;
begin
  result := ItemIndex < High(data);
end;

end.
